/*
 * @Author: Jimmy
 * @LastModifiedBy: Jimmy
 * @Date: 2024-02-25 10:12:39
 * @LastEditTime: 2024-06-22 21:11:16
 * @FilePath: /nest_study/web/src/utils/request.ts
 */
import axios, { AxiosInstance, AxiosRequestConfig, AxiosError, AxiosResponse } from "axios";
import { message } from 'antd'
import { getCache, setCache } from "./storage";
const service: AxiosInstance = axios.create({
    baseURL: import.meta.env.VITE_API_URL,
    timeout: 60 * 1000
});
// 请求处理
service.interceptors.request.use((config: any) => {
    let token = getCache('token');
    config.headers = {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${token}`
    }
    // if (config.headers.isLoading !== false) {
    //     store.dispatch({
    //         type: GlobalTypes.SHOW_LOADING
    //     })
    // }
    return config;
}, (error: AxiosError) => {
    // if (error.config.headers.isLoading !== false) {
    //     store.dispatch({
    //         type: GlobalTypes.HIDE_LOADING
    //     })
    // }
    return Promise.reject(error);
});
//响应处理
service.interceptors.response.use((response: AxiosResponse) => {
    // if (response.config.headers.isLoading !== false) {
    //     store.dispatch({
    //         type: GlobalTypes.HIDE_LOADING
    //     })
    // }
    let { code = 200 } = response.data;
    if (code == 200) {
        return Promise.resolve(response.data);
    } else {
        message.error(response.data.msg || '操作错误');
        if (code == 401) {
            setCache('token', '');
            setTimeout(()=>{
                window.location.replace('/login');
            },1000)
        }
        return Promise.reject(response);
    }
}, (error: any) => {
    // if (error.config.headers.isLoading !== false) {
    //     store.dispatch({
    //         type: GlobalTypes.HIDE_LOADING
    //     })
    // }
    if (error && error.response) {
        switch (error.response.status) {
            case 400:
                message.error(error?.response?.data?.message || error.response.data?.msg || '请求错误')
                break;
            case 401:
                message.error('未授权');
                setCache('token', '');
                window.location.replace('/login');
                break;
            case 403:
                message.error('登录过期')
                setCache('token', '');
                window.location.replace('/login');
                break;
            default:
                message.error(error.response.data.message || error.response.data.msg || '请求错误')
                // window.location.replace('/login');
                break;
        }
    }
    return Promise.reject(error);
});

export const http = {
    get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
        return service.get(url, config)
    },
    delete<T = any>(url: string, config?: AxiosRequestConfig<T>): Promise<T> {
        return service.delete(url, config)
    },
    post<T = any>(url: string, data?: object, config?: AxiosRequestConfig): Promise<T> {
        return service.post(url, data, config)
    },
    put<T = any>(url: string, data?: object, config?: AxiosRequestConfig): Promise<T> {
        return service.put(url, data, config)
    }
}


export default service